﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;

namespace GameApplication.MiniGame.SimpleGame
{
    /// <summary>
    ///     Логика взаимодействия для SliderMiniGame.xaml
    /// </summary>
    public partial class SliderMiniGame : Page, ISimpleGame
    {
        private int _widrectHor;

        private ThicknessAnimation ani;

        public SliderMiniGame()
        {
            InitializeComponent();
        }

        private void Grid_KeyDown_1(object sender, KeyEventArgs e)
        {
            MessageBox.Show("ЗГЫР");

            if (e.Key == Key.Q)
            {
                //MessageBox.Show("ЗГЫР");
                bool win = false;

                if ((ell.Margin.Left > _widrectHor/2 + 20) && (ell.Margin.Left < _widrectHor/2 - 20))
                {
                    MessageBox.Show("Yeap");
                    win = true;
                }
                else
                {
                    MessageBox.Show("Fail");
                    ell.BeginAnimation(MarginProperty, null);
                }

                OnEndBattle(win);
            }
            else
            {
                MessageBox.Show("NoNoNoNo!");
            }
        }


        public event Action<bool> EndBattle;

        protected virtual void OnEndBattle(bool win)
        {
            Action<bool> handler = EndBattle;
            if (handler != null) handler(win);
        }

        private void Grid_Loaded_1(object sender, RoutedEventArgs e)
        {
            _widrectHor = (int) gridrecthor.ActualWidth - 50;

            ani = new ThicknessAnimation();
            double leftcircl = ell.Margin.Left;
            double righttcircl = ell.Margin.Right;
            double botcircl = ell.Margin.Bottom;
            double toptcircl = ell.Margin.Top;
            ani.From = new Thickness(leftcircl, toptcircl, righttcircl, toptcircl);
            ani.To = new Thickness(leftcircl + _widrectHor, toptcircl, righttcircl, toptcircl);

            ani.AutoReverse = true;
            ani.RepeatBehavior = new RepeatBehavior(2);
            ani.RepeatBehavior = RepeatBehavior.Forever;
            ell.BeginAnimation(MarginProperty, ani);
        }
    }
}